package com.qf.main.pharmacy.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.main.pharmacy.common.qo.AuditQo;
import com.qf.main.pharmacy.common.qo.StorageQo;
import com.qf.main.pharmacy.common.vo.AuditVo;
import com.qf.main.pharmacy.common.vo.PharmacyVO;
import com.qf.main.pharmacy.common.vo.StorageVo;
import com.qf.main.pharmacy.entity.Audit;
import com.qf.main.pharmacy.entity.PharmacyInventory;
import com.qf.main.pharmacy.entity.Storage;
import com.qf.main.pharmacy.mapper.PharmacyInventoryMapper;
import com.qf.main.pharmacy.mapper.StorageMapper;
import com.qf.main.pharmacy.service.StorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author chengqiang
 * @Create 2024-03-03-17:43
 * @Description:
 */
@Service
public class StorageServiceImpl implements StorageService {

    @Resource
    private StorageMapper storageMapper;
    @Resource
    private PharmacyInventoryMapper pharmacyInventoryMapper;
    @Override
    @Transactional
    public Integer saveStorage(StorageQo storageQo) {
        Storage storage = CommonBeanutils.convertTo(storageQo,Storage::new);
        storage.setPrice(new BigDecimal(storageQo.getPrice()));
        storage.setTotalPrice(new BigDecimal(storageQo.getTotalPrice()));
        Snowflake snowflake = new Snowflake();
        Long no = snowflake.nextId();
        storage.setStorageNo(no);
        storageMapper.insert(storage);

        pharmacyInventoryMapper.increaseStockQuantity(storage.getPharmacyId(),storage.getQuantity());
        return 1;
    }

    @Override
    public List<AuditVo> orderByIdList(int pageNum, int pageSize, int id) {
        return null;
    }

    @Override
    public PageInfo<StorageVo> storageList(Integer pageNum, Integer pageSize,String storageNo) {
        QueryWrapper<Storage> qw = new QueryWrapper<>();
        qw.eq(!StringUtils.isEmpty(storageNo),Storage.COL_STORAGE_NO,storageNo);
        PageInfo<StorageVo> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(storageMapper.selectList(qw), StorageVo::new));
        return PageInfo;
    }

    @Override
    public Integer modifyStorage(StorageQo storageQo) {
        return null;
    }

    @Override
    public Integer removeStorage(int id) {
        return null;
    }
}
